package com.animee.tally.util;
import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.Display;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;
import androidx.annotation.NonNull;
import com.animee.tally.R;
public class BeizhuDialog extends Dialog implements View.OnClickListener {
    Context context;
    EditText et;    //初始化控件
    Button cancelBtn,ensureBtn;

    public BeizhuDialog(@NonNull Context context) {
        super(context);
        this.context = context;
    }
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_beizhu);   //设置显示布局
        et = findViewById(R.id.dialog_beizhu_et);
        cancelBtn = findViewById(R.id.dialog_beizhu_btn_cancel);
        ensureBtn = findViewById(R.id.dialog_beizhu_btn_ensure);

        cancelBtn.setOnClickListener(this);
        ensureBtn.setOnClickListener(this);
    }

    OnCancelListener onCancelListener;
    OnEnsureListener onEnsureListener;
    public interface OnCancelListener{
        public void onCancel();
    }
    public interface OnEnsureListener{
        public void onEnsure();
    }
    // 设置回调接口
    public void setOnCancelListener(OnCancelListener onCancelListener) {
        this.onCancelListener = onCancelListener;
    }
    public void setOnEnsureListener(OnEnsureListener onEnsureListener) {
        this.onEnsureListener = onEnsureListener;
    }
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.dialog_beizhu_btn_cancel:
                cancel();
                if (onCancelListener!=null) {
                    onCancelListener.onCancel();
                }
                break;
            case R.id.dialog_beizhu_btn_ensure:
                if (onEnsureListener!=null) {
                    onEnsureListener.onEnsure();
                }
                break;
        }
    }

    /* 设置Dialog的尺寸与屏幕尺寸一致*/
    public void setDialogSize(){
        Window window = getWindow();   //获取当前窗口对象
        WindowManager.LayoutParams wlp = window.getAttributes();   //获得窗口对象的参数
        Display d = window.getWindowManager().getDefaultDisplay();//获取屏幕宽
        wlp.width = (int) (d.getWidth());//宽度按屏幕大小的百分比设置 ;d.getWidth()这个方法已废弃
        wlp.gravity = Gravity.BOTTOM;
        window.setBackgroundDrawableResource(android.R.color.transparent);  // 设置窗口的背景资源
        window.setAttributes(wlp);
        handler.sendEmptyMessageDelayed(1,100);
    }
     Handler handler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            if (msg.what==1) {
                //  自动弹出软件盘
                InputMethodManager inputMethodManager = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
                inputMethodManager.toggleSoftInput(0, InputMethodManager.HIDE_NOT_ALWAYS);
            }
          }
    };

    // 获取输入的备注的内容
    public String getEditText(){
        return  et.getText().toString().trim();
    }
}
